<template>
  <div class="app-container">
    <el-row>
      <!-- 搜索区域 -->
      <el-card style="margin:0 0 5px 5px" v-show="showSearch">
        <el-form ref="queryForm" :inline="true" :model="queryData">
          <el-form-item label="订单号" prop="Q_orderSn_S_LK">
            <el-input v-model="queryData.Q_orderSn_S_LK" placeholder="订单号" />
          </el-form-item>
          <el-form-item label="支付方式" prop="Q_payType_S_EQ">
            <el-select v-model="queryData.Q_payType_S_EQ" placeholder="请选择支付方式">
              <el-option label="全部" value="" />
              <el-option
                v-for="item in optionOfChannel"
                :key="item.dictCode"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="订单状态" prop="Q_status_S_EQ">
            <el-select v-model="queryData.Q_status_S_EQ" placeholder="状态">
              <el-option label="全部" value="" />
              <el-option
                v-for="item in optionOfStatus"
                :key="item.dictCode"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="有效" prop="Q_deleteStatus_S_EQ">
            <el-select v-model="queryData.Q_deleteStatus_S_EQ" placeholder="状态">
              <el-option label="全部" value="" />
              <el-option
                v-for="item in optionOfDeleteStatus"
                :key="item.dictCode"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item class="fr p-r-10">
            <el-button class="filter-item" type="primary" round icon="el-icon-search" @click="handleFilter">
              搜索
            </el-button>
            <el-button class="filter-item m-l-10" type="info" round icon="el-icon-refresh" @click="handleClear">
              清空
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- table区域 -->
      <el-card class="m-l-5">
        <el-row :gutter="10" class="m-b-5">
          <table-toolbar :showSearch.sync="showSearch" @refresh="getTableData" />
        </el-row>
        <el-table
          ref="queryTable"
          v-loading="tableLoading"
          row-key="id"
          :data="tableData"
          element-loading-text="加载中"
          border
          fit
          highlight-current-row
          show-overflow-tooltip
          height="100px"
          v-el-height-adaptive-table="{bottomOffset: 110}"
          @sort-change="sortChange"
        >
          <el-table-column align="center" label="订单号" prop="orderSn" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span style="cursor: pointer" @click="handleOrderInfo(scope.row.id)">{{ scope.row.orderSn }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="用户名" prop="username" width="150" show-overflow-tooltip />
          <el-table-column align="center" label="订单总额" prop="totalAmount" width="90" />
          <el-table-column align="center" label="应付总额" prop="payAmount" width="90" />
          <el-table-column align="center" label="运费金额" prop="freightAmount" width="90" />
          <el-table-column align="center" label="支付方式" prop="payType" width="120">
            <template slot-scope="scope">
              <el-link :type="getDictColor('channel', scope.row.payType)" :underline="false">
                {{ getDictValue('channel', scope.row.payType) }}
              </el-link>
            </template>
          </el-table-column>
          <el-table-column align="center" label="订单状态" prop="status" sortable width="120">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status" :type="getDictType('status', scope.row.status)" v-text="getDictValue('status', scope.row.status)" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="收货状态" prop="confirmStatus" sortable width="120">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.confirmStatus" :type="getDictType('confirmStatus', scope.row.confirmStatus)" v-text="getDictValue('confirmStatus', scope.row.confirmStatus)" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="有效" prop="deleteStatus" sortable width="90">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.deleteStatus" :type="getDictType('deleteStatus', scope.row.deleteStatus)" v-text="getDictValue('deleteStatus', scope.row.deleteStatus)" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="支付时间" prop="paymentTime" width="180" show-overflow-tooltip />
          <el-table-column align="center" label="创建时间" prop="createTime" width="180" show-overflow-tooltip />
          <el-table-column align="center" label="备注" prop="remark" />
          <el-table-column align="center" label="操作" width="90" fixed="right">
            <template slot-scope="scope">
              <el-dropdown>
                <span class="el-dropdown-link">
                  选项<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item>
                    <el-link icon="el-icon-view" @click="handleOrderInfo(scope.row.id)">查看</el-link>
                  </el-dropdown-item>
                  <el-dropdown-item v-if="scope.row.status !== '1'">
                    <el-link icon="el-icon-view" @click="handleOrderCheck(scope.row.orderSn)">查看付款情况</el-link>
                  </el-dropdown-item>
                  <el-dropdown-item v-if="scope.row.billType == '1' && scope.row.status == '3'">
                    <el-link icon="el-icon-download" @click="handleOrderBill(scope.row.id)">电子发票</el-link>
                  </el-dropdown-item>
                  <el-dropdown-item v-permission="['mall:order:delete']">
                    <el-link type="danger" icon="el-icon-delete" @click="handleDelete(scope.row.id)">删除</el-link>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="tableQuery.total>0" :total="tableQuery.total" :page.sync="tableQuery.page" :limit.sync="tableQuery.limit" @pagination="getTableData" />
      </el-card>
    </el-row>

    <order-detail-dialog v-if="openFormDialog" ref="detail_form" />
  </div>
</template>

<script>
import { orderTable, orderDelete, orderCheck, orderVoice } from '@/api/mall/order/order'
import OrderDetailDialog from './form/dialog'

export default {
  components: {
    OrderDetailDialog
  },
  data() {
    return {
      showSearch: true,
      queryData: {
        Q_userId_L_EQ: undefined,
        Q_orderSn_S_LK: undefined,
        Q_payType_S_EQ: undefined,
        Q_status_S_EQ: undefined,
        Q_deleteStatus_S_EQ: '0'
      },
      optionOfChannel: [],
      optionOfStatus: [],
      optionOfConfirmStatus: [],
      optionOfDeleteStatus: [],
      tableLoading: false,
      tableData: [],
      tableQuery: {
        page: 1,
        limit: 20,
        total: 0,
        sort: undefined,
        order: undefined
      },
      openFormDialog: false
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      const userId = this.$route.query.userId
      if (userId) {
        this.queryData.Q_userId_L_EQ = userId
      }
      this.$nextTick(() => {
        this.getDictData()
      })
    },
    getDictData() {
      const that = this
      this.$http.all([this.$dictGroup('sys_pay_type'), this.$dictGroup('sys_order_status'),
        this.$dictGroup('sys_confirm_status'), this.$dictGroup('sys_yes_no')])
        .then(this.$http.spread(function(response1, response2, response3, response4) {
          that.optionOfChannel = response1
          that.optionOfStatus = response2
          that.optionOfConfirmStatus = response3
          that.optionOfDeleteStatus = response4
          that.getTableData()
        }))
    },
    getDictType(property, value) {
      return this.getDictElement(property, value, 0)
    },
    getDictValue(property, value) {
      return this.getDictElement(property, value, 1)
    },
    getDictColor(property, value) {
      return this.getDictElement(property, value, 2)
    },
    getDictElement(property, value, index) {
      if (property === 'channel') {
        return this.$api.getDictElement(this.optionOfChannel, value)[index]
      } else if (property === 'status') {
        return this.$api.getDictElement(this.optionOfStatus, value)[index]
      } else if (property === 'confirmStatus') {
        return this.$api.getDictElement(this.optionOfConfirmStatus, value)[index]
      } else if (property === 'deleteStatus') {
        return this.$api.getDictElement(this.optionOfDeleteStatus, value)[index]
      }
    },
    handleFilter() {
      this.tableQuery.page = 1
      this.getTableData()
    },
    handleClear() {
      this.$refs.queryForm.resetFields()
      this.getTableData()
    },
    getTableData() {
      const query = Object.assign(this.tableQuery, this.queryData)
      this.tableLoading = true
      orderTable(query).then(response => {
        this.tableData = response.param.data
        this.tableQuery.total = response.param.recordsTotal
        this.tableLoading = false
        this.$refs.queryTable.bodyWrapper.scrollTop = 0
      }).catch(e => {
        this.tableLoading = false
      })
    },
    sortChange(data) {
      const { prop, order } = data
      this.tableQuery.sort = prop
      if (order === 'ascending') {
        this.tableQuery.order = 'asc'
      } else {
        this.tableQuery.order = 'desc'
      }
      this.getTableData()
    },
    handleOrderInfo(id) {
      this.openFormDialog = true
      this.$nextTick(() => {
        this.$refs.detail_form.init(id)
      })
    },
    handleOrderCheck(orderSn) {
      orderCheck(orderSn).then(response => {
        this.$modal.msgSuccess(response.msg)
      }).catch()
    },
    handleOrderBill(id) {
      this.$modal.loading('正在生成发票...')
      orderVoice(id).then(response => {
        window.location.href = this.$store.state.app.server_api + `${process.env.VUE_APP_MODULE_ADMIN_API}/public/file/download/${response.param}?delete=true`
        this.$modal.closeLoading()
      }).catch(e => { this.$modal.closeLoading() })
    },
    handleDelete(id) {
      this.$confirm('是否删除所选订单?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        orderDelete(id).then(response => {
          this.handleFilter()
          this.$modal.msgSuccess(response.msg)
        }).catch(e => {})
      }).catch(e => {})
    }
  }
}
</script>
